﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;

namespace ShowSystem
{
    class Object2D : AllObject
    {
        #region Data

        public Texture2D texture;

        private Vector2 position;
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        private Vector2 velocity;
        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        private float rotation;
        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        private Vector2 rotationPosition;
        public Vector2 RotationPosition
        {
            get { return rotationPosition; }
            set { rotationPosition = value; }
        }

        /* Define the size of every picture */
        private Point frameSize;
        public Point FrameSize
        {
            get { return frameSize; }
            set { frameSize = value; }
        }

        /* Define the size of the picture's sheet */
        private Point sheetSize;
        public Point SheetSize
        {
            get { return sheetSize; }
            set { sheetSize = value; }
        }

        /* Define the current using point in picture's sheet */
        private Point currentPoint;
        public Point CurrentPoint
        {
            get { return currentPoint; }
            set { currentPoint = value; }
        }

        private float scale;
        public float Scale
        {
            get { return scale; }
            set { scale = value; }
        }

        public Color color;

        /* (0,1) */
        private float layer;
        public float Layer
        {
            get { return layer; }
            set { layer = value; }
        }

        /* Define the time of every frame */
        private int perFrame;
        public int PerFrame
        {
            get { return perFrame; }
            set { perFrame = value; }
        }

        private int currentPastTime;
        private int CurrentPastTime
        {
            get { return currentPastTime; }
            set { currentPastTime = value; }
        }

        #endregion

        public Object2D(Texture2D texture, Point frameSize, Point sheetSize,int perFrame)
        {
            this.texture = texture;
            position = Vector2.Zero;
            velocity = Vector2.Zero;
            this.frameSize = frameSize;
            this.sheetSize = sheetSize;
            currentPoint = Point.Zero;
            rotation = 0;
            rotationPosition = Vector2.Zero;
            scale = 1.0f;
            color = Color.White;
            layer = 0;
            this.perFrame = perFrame;
        }

        public virtual void Initialize(Vector2 position, Vector2 velocity, float scale,float layer)
        {
            this.position = position;
            this.velocity = velocity;
            this.scale = scale;
            this.layer = layer;
            currentPastTime = 0;
        }

        public virtual void Update(GameTime gameTime)
        {
            if (IsAlive)
            {
                currentPastTime += gameTime.ElapsedGameTime.Milliseconds;
                if (currentPastTime >= perFrame)
                {
                    currentPastTime = 0;
                    if (currentPoint.Y + 1 < sheetSize.Y)
                        currentPoint.Y++;
                    else
                        if (currentPoint.X + 1 < sheetSize.X)
                        {
                            currentPoint.Y = 0;
                            currentPoint.X++;
                        }
                        else
                            currentPoint = Point.Zero;
                }
            }
        }

        public virtual void Draw(SpriteBatch spriteBatch,SpriteFont spriteFont)
        {
            if (IsVisible)
            {
                spriteBatch.Draw(texture, position,
                    new Rectangle(currentPoint.X * frameSize.X, currentPoint.Y * frameSize.Y, frameSize.X, frameSize.Y),
                    color, rotation, rotationPosition, scale, SpriteEffects.None, layer);
                spriteBatch.DrawString(spriteFont, Word, WordPosition, WordColor);
            }
        }
    }
}
